<%@page isELIgnored="false" pageEncoding="UTF-8" contentType="text/html; UTF-8" %>


<table id="cmfzalbumtree" style="width:600px;height:400px"></table>

<!--添加的对话框表  -->
<div id="addAlbumDtoDialog" >
    <form id="addAlbumDtoform" enctype="multipart/form-data" method="post">
        图片：<input type="file" value="" name="Image" onchange="selectImage(this);">
        <div class="lf salebd">
            <a href="#"><img id="addAlbumDtoDialogImage" src="images/ad20.jpg" width="100" height="100" /></a>
        </div><br/>
        专辑名称：<input  name="albumName"><br/>
        作者：<input  name="albumAuthor"><br/>
        播音员：<input  name="albumTeller"><br/>
        专辑简介：<input  name="albumContent"><br/>
        专辑评价：1星<input type="radio" name="albumStar" value="1"><br/>
        2星<input type="radio" name="albumStar" value="2"><br/>
        3星<input type="radio" name="albumStar" value="3"><br/>
        4星<input type="radio" name="albumStar" value="4"><br/>
        5星<input type="radio" name="albumStar" value="5"><br/>
    </form>
</div>
<!--添加的对话框表 -----------end -->


<div id="addAudioDialog" >
    <form id="addAudioDialogForm" enctype="multipart/form-data" method="post">
        音频：<input type="file" id="file2" value="" name="addAudioImage" onchange="selectImage2(this);">
        <div class="lf salebd">
            <a href="#"><img id="addAudioImage" src="" width="100" height="100" /></a>
        </div><br/>
        音频名称：<input id="audioName"  name="audioName"><br/>


        <input id="audioAlbumId"   name="AlbumId" style="display: none"><br/>
        <input id="audioSize"   name="audioSize" style="display: none"><br/>

    </form>
</div>


<script>
    $(function () {
        $("#addAlbumDtoDialog").dialog({
            title: "添加框",
            width: 600,
            height: 300,
            closed: true,
            buttons:[{
                text:"添加",
                handler:function(){
                    doaddAlbumDtoDialog();
                },
            }],
        });
        /*添加音频*/
        $("#addAudioDialog").dialog({
            title: "添加框",
            width: 600,
            height: 300,
            closed: true,
            buttons:[{
                text:"添加",
                handler:function(){
                    doaddAudioDialog();
                },
            }],
        });


        $('#cmfzalbumtree').treegrid({
            fit:true,
            url:'${pageContext.request.contextPath}/AlbumDtoController/selectAllAlbumDao.do',
            idField:'id',
            treeField:'name',
            method: "get",
            checkbox:true,
            onDblClickCell:function (field, row) {
                console.log(row);
                $("#audioAlbumId").val(row.id);
                $("#addAudioDialog").panel('open');
            },
            columns:[[
                {field: 'name',title: '章节名字',  width: 180},
                {field: 'childrenAlbumId', title: '专辑ID', width: 80},
                {field: 'audioUrl', title: '下载路径', width: 80,formatter:function(value,row,index){
                    {
                        return "<input type='button' onclick='doxiazai(row.audioUrl);' value='点击下载' />";
                    }
                }
                },
                {field: 'audioSize', title: '章节大小', width: 80},
                {field: 'audioOrder', title: '排序', width: 80},
            ]],
            toolbar: [{
                iconCls: 'icon-edit',
                text:"添加专辑",
                handler: function(){
                    $("#addAlbumDtoDialog").panel('open');
                }
            },'-',{
                iconCls: 'icon-help',
                text:"删除选中",
                handler: function(){
                    $.messager.confirm("确认框", "您确认想要删除记录吗？", function (r) {
                        if (r) {
                            /*获取选中的行对象  */
                            var hang = $("#cmfzalbumtree").datagrid("getSelections");
                            console.log(hang);
                            var ids = new Array();
                            for (var i = 0; i < hang.length; i++) {
                                ids[i] = hang[i].id;
                            }
                            console.log(ids);

                        }
                    });
                }
            }]

        });
    })


    function doaddAlbumDtoDialog(){
        $("#addAlbumDtoform").form("submit",{
            url:"${pageContext.request.contextPath}/Album/addAlbum.do",
            onSubmit: function(){
                //提交数据前需要的判断
                return true;
            },
            success:function(data){
                var data = eval('(' + data + ')');
                if(data.isok){
                    /*添加成功弹出对话框  */
                    $.messager.alert("消息","添加成功");
                    /*关闭对话框*/
                    $("#addAlbumDtoDialog").dialog('close');
                    /*刷新table  */
                    $('#cmfzalbumtree').treegrid('reload');
                }else{
                    $.messager.alert("消息","添加失败");
                };
            }

        });
    }

    function doaddAudioDialog(){
        $("#addAudioDialogForm").form("submit",{
            url:"${pageContext.request.contextPath}/AlbumDtoController/addAudio.do",
            onSubmit: function(){
                //提交数据前需要的判断
                return true;
            },
            success:function(data){
                var data = eval('(' + data + ')');
                if(data.isok){
                    /*添加成功弹出对话框  */
                    $.messager.alert("消息","添加成功");
                    /*关闭对话框*/
                    $("#doaddAudioDialog").dialog('close');
                    /*刷新table  */
                    $("#allAlbum").datagrid("reload");
                }else{
                    $.messager.alert("消息","添加失败");
                };
            }
        });
    }


    function doxiazai(){
        console.log("000----------000");
        console.log(row);
    }

</script>
<%--添加专辑--%>
<script type="text/javascript">
    var image = '';
    function selectImage(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById('addAlbumDtoDialogImage').src = evt.target.result;
            image = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);
    }
</script>
<%--添加音频--%>
<script type="text/javascript">
    var image = '';
    function selectImage2(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById('addAudioImage').src = evt.target.result;
            image = evt.target.result;
        }
        reader.readAsDataURL(file.files[0]);

        var audioSize = ($("#file2")[0].files[0].size)/1024/1024;
        resultsize = Math.round(audioSize * 100) / 100;
        var s_x = resultsize.toString();
        var pos_decimal = s_x.indexOf('.');
        if (pos_decimal < 0) {
            pos_decimal = s_x.length;
            s_x += '.';
        }
        while (s_x.length <= pos_decimal + 2) {
            s_x += '0';
        }
        $("#audioSize").val(s_x+"M");
    }
</script>